package org.lanqiao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lanqiao.entity.User;
import org.lanqiao.service.UserService;
import org.lanqiao.util.Result;
import org.lanqiao.util.ResultEnum;
import org.lanqiao.util.SessionListener;
import org.lanqiao.util.UUId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description 获取User表数据
 * @author: 李静
 * @date: 2019/5/13
 * @version: 1.0
 */
@Controller
@ResponseBody
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    //查询全部
    @RequestMapping(value = "/queryUserList")
    public Result queryUserList(@RequestParam(value = "id", defaultValue = "1") Integer id) {

        //使用PageHelper静态方法调用开始分页
        PageHelper.startPage(id, 15);
        List<User> list = userService.queryUserList();
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return Result.ok(pageInfo);
    }


    /**
     * @return org.lanqiao.entity.User
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/5/20 17:44
     * @Param []
     * @explain 根据用户名字查询用户所有信息
     */


    @RequestMapping(value = "/selectByName")
    public Result selectByName(String userName) {
        if (userName.length() > 5) {
            return Result.defeated("用户名太长");
        }

        return userService.selectByName(userName);
    }

    /**
     * @return
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 2019/5/22 15:00
     * @Param 用户的添加
     */
    @RequestMapping("/addUser")
    public Result addUser(User user) {
        return userService.addUser(user);
    }

    /**
     * @return org.lanqiao.entity.Result
     * @Author 王宇
     * @Description //TODO 通过邮箱密码查询用户(用于登录)
     * @Date 2019/05/23 9:39
     * @Param [userEmail, userPassword]
     */
    @RequestMapping("/getUser")
    public Result getUser(String userEmail, String userPassword) {
        return userService.getUser(userEmail, userPassword);
    }


    /**
     * @return org.lanqiao.util.Result
     * @Author 徐金星
     * @Description //TODO
     * @Date 2019/5/23 11:22
     * @Param [str, id]
     * @explain
     */
    @RequestMapping(value = "/updateUsersStatus")
    public Result usersStatus(String str, String id, @RequestParam(value = "ids", defaultValue = "1") Integer ids) {
        if (str.equals("sethonor")) {
            userService.updateUserStatusF(id);
            PageHelper.startPage(ids, 15);
            List<User> list = userService.queryUserList();
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return Result.ok(pageInfo);
        } else if (str.equals("deknight")) {
            userService.updateUserStatusJ(id);
            PageHelper.startPage(ids, 15);
            List<User> list = userService.queryUserList();
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return Result.ok(pageInfo);
        } else {
            return Result.defeated("");
        }
    }

    /**
     * 任春旭：查询用户信息
     */
    @RequestMapping(value = "/queryUser")
    public Result queryUser(String userEmail) {
        return userService.queryUser(userEmail);
    }


    /**
     * 任春旭：修改用户名称
     */
    @RequestMapping(value = "/upName")
    public Result upName(String date, String userEmail) {
        return userService.upName(date, userEmail);
    }

    /**
     * 任春旭：修改用户邮箱
     */
    @RequestMapping(value = "/upEmail")
    public Result upEmail(String date, String userId) {
        //查找邮箱
        //mailRegex是整体邮箱正则表达式，mailName是@前面的名称部分，mailDomain是后面的域名部分
        String mailRegex, mailName, mailDomain;
        mailName = "^[0-9a-z]+\\w*";       //^表明一行以什么开头；^[0-9a-z]表明要以数字或小写字母开头；\\w*表明匹配任意个大写小写字母或数字或下划线
        mailDomain = "([0-9a-z]+\\.)+[0-9a-z]+$";       //***.***.***格式的域名，其中*为小写字母或数字;第一个括号代表有至少一个***.匹配单元，而[0-9a-z]$表明以小写字母或数字结尾
        mailRegex = mailName + "@" + mailDomain;          //邮箱正则表达式      ^[0-9a-z]+\w*@([0-9a-z]+\.)+[0-9a-z]+$
        Pattern compile = Pattern.compile(mailRegex);
        Matcher matcher = compile.matcher(date);
        if (matcher.matches()) {
            System.out.println("正确");
            return userService.upEmail(date, userId);
        } else {
            return Result.defeated("");
        }
//        return userService.upEmail(date, userId);
    }

    /**
     * 任春旭：修改用户密码
     */
    @RequestMapping(value = "/upPassword")
    public Result upPassword(String date, String userEmail) {
        return userService.upPassword(date, userEmail);
    }

    /**
     * @return
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 2019/6/5 11:02
     * @Param 退出
     */
    @RequestMapping(value = "/userExit")
    public Result userExit(String userEmail) {
        return userService.userExit(userEmail);
    }
}
